package com.wechat.back.service.impl;

import com.wechat.back.mapper.MsgMapper;
import com.wechat.back.pojo.inform.Message;
import com.wechat.back.pojo.inform.MsgState;
import com.wechat.back.service.MsgService;
import com.wechat.back.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class MsgServiceImpl implements MsgService {
    @Autowired
    private MsgMapper msgMapper;

    @Override
    public List<Message> findBySign(Integer msgSign) {
        return msgMapper.findBySign(msgSign);
    }

    @Override
    public List<Message> findAll() {
        return msgMapper.findAll();
    }

    @Override
    public void addMsg(Message message) {
        message.setMsgTime(LocalDateTime.now());
        msgMapper.addMsg(message);
    }

    @Override
    public void addMsgState() {
        MsgState msgState = new MsgState();
        Map<String, Object> map = ThreadLocalUtil.get();
        msgState.setUserId((Integer) map.get("id"));
        msgState.setUser(map.get("username").toString());
        msgState.setCreateTime(LocalDateTime.now());
        // 判断用户是否已经存在
        if(msgMapper.findMsgStateByUserId(msgState.getUserId())==null){
            msgMapper.addMsgState(msgState);
        }
    }

    @Override
    public MsgState findMsgStateByUserId(Integer userId) {
        return msgMapper.findMsgStateByUserId(userId);
    }

    @Override
    public List<MsgState> findAllMsgState(String user) {
        return msgMapper.findAllMsgState(user);
    }
}
